package com.example.himalaya2.presenters;

import com.example.himalaya2.api.XimalayaApi;
import com.example.himalaya2.interfaces.ISearchCallback;
import com.example.himalaya2.interfaces.ISearchPresenter;
import com.example.himalaya2.utils.LogUtil;
import com.ximalaya.ting.android.opensdk.datatrasfer.IDataCallBack;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import com.ximalaya.ting.android.opensdk.model.album.SearchAlbumList;
import com.ximalaya.ting.android.opensdk.model.word.AlbumResult;
import com.ximalaya.ting.android.opensdk.model.word.HotWord;
import com.ximalaya.ting.android.opensdk.model.word.HotWordList;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;
import com.ximalaya.ting.android.opensdk.model.word.SuggestWords;
import com.example.himalaya2.utils.Constants;
import java.util.ArrayList;
import java.util.List;

public class SearchPresenters implements ISearchPresenter {
    private List<ISearchCallback>  mCallbacks = new ArrayList<>();
    private String mCurrentKeyWord = null;
    private  XimalayaApi mXimalayaApi;
    private static final int DEFAULT_PAGE = 1;
    private int mCurrentPage = DEFAULT_PAGE;
    private static final String TAG = "SearchPresenters";
    private List<Album> mSearchResult = new ArrayList<>();


    private SearchPresenters(){
        mXimalayaApi = XimalayaApi.getInstance();
    };

    private static SearchPresenters sSearchPresenters = null;

    public static SearchPresenters getInstance() {
        if (sSearchPresenters == null) {
            synchronized (SearchPresenters.class) {
                if (sSearchPresenters == null) {
                    sSearchPresenters = new SearchPresenters();
                }
            }
        }
        return sSearchPresenters ;
    }

    @Override
    public void doSearch(String keyword) {
        mCurrentPage = DEFAULT_PAGE;
        mSearchResult.clear();
        // 当网络不好的时候 ，用户会点击重新搜索
        this.mCurrentKeyWord = keyword;
        search(keyword);
    }

    private void search(String keyword) {
        mXimalayaApi.searchByKeyWord(keyword, mCurrentPage, new IDataCallBack<SearchAlbumList>() {
            @Override
            public void onSuccess(SearchAlbumList searchAlbumList) {
                List<Album> albumList = searchAlbumList.getAlbums();
                mSearchResult.addAll(albumList);
                LogUtil.d(TAG," searchAlbumList :"+albumList.toString());

                if (albumList != null) {
                    if (isLoadMore) {
                        for (ISearchCallback callback : mCallbacks) {
                            if (albumList.size() == 0) {
                                callback.onLoadMoreResult(mSearchResult,false);
                            } else {
                                callback.onLoadMoreResult(mSearchResult,true);
                            }
                        }
                        isLoadMore = false;
                    } else {
                        for (ISearchCallback callback : mCallbacks) {
                            callback.onSearchResultLoad(mSearchResult);
                        }
                    }
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.d(TAG," searchAlbumList 错误代码 ："+i+"  错误消息  ："+s);

                for (ISearchCallback callback : mCallbacks) {
                    if (isLoadMore) {
                        callback.onLoadMoreResult(mSearchResult,true);
                        mCurrentPage -- ;
                        isLoadMore = false;
                    } else {
                        callback.onError(i,s);
                    }
                }

            }
        });
    }

    @Override
    public void reSearch() {
        search(mCurrentKeyWord);
    }

    private boolean isLoadMore = false;

    @Override
    public void loadMore() {
        // 判断有没有必要加载更多
        if (mSearchResult.size() < Constants.COUNT_DEFAULT) {
            for (ISearchCallback callback : mCallbacks) {
                callback.onLoadMoreResult(mSearchResult,false);
            }
        } else {
            isLoadMore = true;
            mCurrentPage ++;
            search(mCurrentKeyWord);
        }
    }

    @Override
    public void getHotWord() {
        mXimalayaApi.getHotWord(new IDataCallBack<HotWordList>() {
            @Override
            public void onSuccess(HotWordList hotWordList) {
                if (hotWordList != null) {
                    List<HotWord> hotWordList1 = hotWordList.getHotWordList();
                    LogUtil.d(TAG," hotWordList1 :"+hotWordList1.toString());
                    for (ISearchCallback callback : mCallbacks) {
                        callback.onHotWordLoad(hotWordList1);
                    }
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.d(TAG," hotWordList 错误代码 ："+i+"  错误消息  ："+s);
                for (ISearchCallback callback : mCallbacks) {
                    callback.onError(i,s);
                }
            }
        });
    }

    @Override
    public void getRecommendWord(String keyword) {
        mXimalayaApi.getSuggestWord(keyword, new IDataCallBack<SuggestWords>() {
            @Override
            public void onSuccess(SuggestWords suggestWords) {
                if (suggestWords != null) {
                    List<QueryResult> suggestWordsAlbumList = suggestWords.getKeyWordList();
                    LogUtil.d(TAG,"suggestWordsAlbumList :"+suggestWordsAlbumList.toString());
                    for (ISearchCallback callback : mCallbacks) {
                        callback.onRecommendWordLoaded(suggestWordsAlbumList);
                    }
                }
            }

            @Override
            public void onError(int i, String s) {
                LogUtil.d(TAG,"suggestWordsAlbumList 错误代码 ："+i+"  错误消息  ："+s);
                for (ISearchCallback callback : mCallbacks) {
                    callback.onError(i,s);
                }
            }
        });
    }

    @Override
    public void registerViewCallback(ISearchCallback iSearchCallback) {
        if (!mCallbacks.contains(iSearchCallback)) {
            mCallbacks.add(iSearchCallback);
        }
    }

    @Override
    public void unRegisterViewCallback(ISearchCallback iSearchCallback) {
        mCallbacks.remove(iSearchCallback);
    }
}
